package yxy.game.pm2.command.camptower;

import org.joda.time.DateTime;
import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.racetowerdate;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Constant;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.CampTowerMessage;
import yxy.game.pm2.module.camptower.CampTowerHandle;
import yxy.game.pm2.module.camptower.CampTowerModule;

import java.util.Map;
import java.util.Set;

/**
 * 阵营塔主界面请求
 * 返回每个阵营塔的结束时间
 *
 * @ClassName CMD_CampTowerUI
 * @Author jingtao-zheng
 * @Date 2022/8/1 10:38
 * @Version 1.0
 **/
@MessageType(proto = Command.CampTowerAllUI, classtype = CampTowerMessage.CampTowerAllUIRequest.class)
public class CMD_CampTowerAllUI extends Command<CampTowerMessage.CampTowerAllUIRequest> {

    @Override
    public void execute(MessageSource messageSource, CampTowerMessage.CampTowerAllUIRequest request) throws Exception {
        String playerCode = messageSource.getTag();
        CampTowerMessage.CampTowerAllUIResponse.Builder response = CampTowerMessage.CampTowerAllUIResponse.newBuilder();
        //获取策划配置的表信息
        Map<String, racetowerdate> campMap = DataCenter.getAllData(racetowerdate.class);
        if (campMap == null) send(messageSource, response, Result.ParamError);
        //获取所有阵营的结束时间
        Map<String, DateTime> allCampTowerEndTime = CampTowerHandle.getAllCampTowerEndTime(campMap);
        Set<Map.Entry<String, DateTime>> entries = allCampTowerEndTime.entrySet();
        CampTowerModule module = CampTowerModule.get(playerCode);

        for (Map.Entry<String, DateTime> entry : entries) {
            //结束时间
            CampTowerMessage.CampTowersEndTime.Builder campTowersEndTime = CampTowerMessage.CampTowersEndTime.newBuilder();
            campTowersEndTime.setEnd(entry.getValue().getMillis());
            //塔类型
            String key = entry.getKey();
            int type = CampTowerHandle.matchRacetowerdateTypeRe(key);
            CampTowerMessage.LineupType lineupType = CampTowerMessage.LineupType.forNumber(type);
            campTowersEndTime.setType(lineupType);
            //获取章节
            Integer chapter = module.getChapter().get(type);
            campTowersEndTime.setChapter(chapter);
            response.addCampTowersEndTime(campTowersEndTime);
        }

        send(messageSource, response, Result.Success);
    }

    private void send(MessageSource messageSource, CampTowerMessage.CampTowerAllUIResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }

    /**
     * 匹配阵营塔类型
     *
     * @param key
     */
    private int matchType(String key) {
        if (key.equals(Constant.WEI_ID)) {
            return Constant.WEI;
        }
        if (key.equals(Constant.SHU_ID)) {
            return Constant.SHU;
        }
        if (key.equals(Constant.WU_ID)) {
            return Constant.WU;
        }
        if (key.equals(Constant.QUN_ID)) {
            return Constant.QUN;
        }
        return 0;
    }
}
